package com.sen.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sen.entity.DruginfoEntity;
import com.sen.entity.WarehouseEntity;
import com.sen.entity.WarehouseLog;
import com.sen.entity.WarehouseLogHead;
import com.sen.service.WarehouseLogHeadService;
import com.sen.service.WarehouseLogService;
import com.sen.service.WarehouseService;
import com.sen.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 库存信息表
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2021-01-08 17:54:40
 */
@RestController
@RequestMapping("warehouse")
public class WarehouseController {
    @Autowired
    private WarehouseService warehouseService;
    @Autowired
    private WarehouseLogService warehouseLogService;
    @Autowired
    private WarehouseLogHeadService warehouseLogHeadService;

    /**
     * 盘点仓库数量
     *
     * @param warehouse
     * @return
     */
    @PostMapping("/updateCount")
    public R updateCount(String warehouse) {
        List<WarehouseEntity> warehouseEntities = JSON.parseArray(warehouse, WarehouseEntity.class);
        //生成记录头信息
        WarehouseLogHead warehouseLogHead = new WarehouseLogHead();
        warehouseLogHead.setEid(warehouseEntities.get(1).getEid());
        warehouseLogHead.setMname(warehouseEntities.get(1).getMname());
        Integer oldSum = 0;
        Integer newSum = 0;
        for (WarehouseEntity warehouseEntity : warehouseEntities) {
            oldSum += warehouseEntity.getCount();
            newSum += warehouseEntity.getNewCount();
        }
        warehouseLogHead.setOldsum(oldSum);
        warehouseLogHead.setNewsum(newSum);
        //添加记录头信息
        warehouseLogHeadService.save(warehouseLogHead);
        for (WarehouseEntity warehouseEntity : warehouseEntities) {
            Integer lno = warehouseEntity.getLno();
            //修改仓库总数量
            warehouseService.update(new UpdateWrapper<WarehouseEntity>()
                    .eq("lno", lno)
                    .set("count", warehouseEntity.getNewCount()));
            //记录修改人和修改数据及时间
            WarehouseLog warehouseLog = new WarehouseLog();
            warehouseLog.setId(warehouseLogHead.getId());
            warehouseLog.setLno(lno);
            warehouseLog.setOldcount(warehouseEntity.getCount());
            warehouseLog.setNewcount(warehouseEntity.getNewCount());
            //添加详细盘点信息
            warehouseLogService.save(warehouseLog);
        }
        return R.ok();
    }

    /**
     * 获取仓库要盘点的药品
     *
     * @param mid
     * @return
     */
    @PostMapping("/queryLnoByMid")
    public R queryLnoByMid(Integer mid) {
        List<WarehouseEntity> warehouseList = warehouseService.queryLnoByMid(mid);

        return R.ok().put("warehouseList", warehouseList);
    }

    /**
     * 查询临期药品
     *
     * @param advent
     * @return
     */
    @PostMapping("/advent")
    public R advent(@RequestParam Map<String, String> advent) {
        Page<DruginfoEntity> page = new Page<>(Integer.parseInt(advent.get("current")), Integer.parseInt(advent.get("size")));
        IPage<DruginfoEntity> drugList = warehouseService.querList(page, advent);
        return R.ok().put("drugList", drugList);
    }

}
